package com.sgb.auction.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.auction.R;
import com.sgb.auction.databinding.AllOfferFragmentBinding;
import com.sgb.auction.entity.AuctionRecordEntity;
import com.sgb.auction.adapter.AllOfferAdapter;
import com.sgb.auction.entity.RequestBean;
import com.sgb.auction.viewmodule.AllOfferModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * Time:2021/11/22
 * AUTHOR:F_Q
 * Description: 竞买记录-全部出价
 */
public class AllOfferFragment  extends AppLazyFragment {

    private RequestBean requestBean = new RequestBean();
    private AllOfferFragmentBinding mBinding;
    private AllOfferModel mModel;
    private AllOfferAdapter allofferadapter;
    private boolean isLoadMore = false;
    private String no = "";

    public static AllOfferFragment newInstance(String no) {
        AllOfferFragment fragment = new AllOfferFragment();
        Bundle bundle = new Bundle();
        bundle.putString("no",no);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.all_offer_fragment, container, false);
        mModel = new ViewModelProvider(this).get(AllOfferModel.class);
        mModel.setActivity(AllOfferFragment.this);
        no = getArguments().getString("no");
        return mBinding.getRoot();
    }


    @Override
    protected void onLazyLoad() {
        initDataList();
    }

    @Override
    protected void initView() {
        super.initView();
        if (allofferadapter == null) {
            allofferadapter = new AllOfferAdapter(getActivity(), null);
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mBinding.recyclerView.setLayoutManager(layoutManager);
        mBinding.recyclerView.setHasFixedSize(true);
        //解决数据加载完成后, 没有停留在顶部的问题
        mBinding.recyclerView.setFocusable(false);
        mBinding.recyclerView.setAdapter(allofferadapter);
        mBinding.recyclerView.setLoadingMoreEnabled(true);
        mBinding.recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initDataList();
            }

            @Override
            public void onLoadMore() {
                isLoadMore = true;
                requestBean.page++;
                //上拉加载更多
                mModel.getAllOfferList(requestBean);
            }
        });
    }

    private void initDataList() {
        isLoadMore = false;
        requestBean.page=1;
        requestBean.setAuctionNo(no);
        mModel.getAllOfferList(requestBean);
    }

    public void setAllOfferList(List<AuctionRecordEntity> data, boolean lastPage) {
        RecyclerRefreshing();
        if (isLoadMore) {
            if (data != null && data.size() > 0) {
                allofferadapter.addDatas(data);
            }
            mBinding.recyclerView.setNoMore(lastPage);
        } else {
            if (data != null && data.size() > 0) {
                mBinding.emptyView.setVisibility(View.GONE);
                mBinding.recyclerView.setVisibility(View.VISIBLE);
                allofferadapter.setDatas(data);
                mBinding.recyclerView.scrollToPosition(0);
                mBinding.recyclerView.setNoMore(lastPage);
            } else {
                allofferadapter.clearDatas();
                mBinding.emptyView.setVisibility(View.VISIBLE);
                mBinding.recyclerView.setVisibility(View.GONE);
            }
        }
    }


    private void RecyclerRefreshing() {
        mBinding.recyclerView.refreshComplete();
        mBinding.recyclerView.loadMoreComplete();
    }
}
